/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.backing;

import com.ez2find.manager.NickManagerLocal;
import com.ez2find.manager.RegisterUserManagerLocal;
import com.ez2find.model.Nick;
import com.ez2find.util.SessionContext;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author org.@le
 */
public class NicksBean {

    @EJB
    NickManagerLocal ejbNicks;
    private Nick nick;
    private Long id;

    /** Creates a new instance of NicksBean */
    public NicksBean() {
    }

    public Nick getNick() {
        if (nick == null) {
            nick = new Nick();
            nick.setPrivacy("1");
            nick.setOrders("0");
            nick.setAnons("0");
        }
        return nick;
    }

    public void setNick(Nick nick) {
        this.nick = nick;
    }

    public String newNick() {
        SessionContext.removeAttribute("NicksBean");
        return "new";
    }

    public boolean handleExists(Nick handle) {
        boolean ans = false;
        for (Nick n : ejbNicks.listAll()) {
            if (n.getName().matches(handle.getName())) {
                ans = true;
            }
        }
        return ans;
    }
    private RegisterUserManagerLocal ejbRegisterUser;

    public String saveNick() {
        if (handleExists(nick)) {
            FacesMessage msg = new FacesMessage("Handle exists, pick another one.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
        try {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            if (getNick().getId() == null) {
                nick.setUser(sessionBean.getEjbSession().getOwner());
                ejbNicks.create(nick);
                sessionBean.getEjbSession().getOwner().getNicks().add(nick);
                System.out.println("Handle created correctly.");
                FacesMessage msg = new FacesMessage("Handle created correctly.");
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            
                return "succes";
            } else {
                ejbNicks.update(nick);
                int index = getIndex(nick, sessionBean.getEjbSession().getOwner().getNicks());
                sessionBean.getEjbSession().getOwner().getNicks().set(index, nick);
                System.out.println("Handle updated correctly.");
                SessionContext.removeAttribute("NicksBean");
                return "succes";
            }


        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Handle already created or user not logged in.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);

            Logger.getLogger(NicksBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public int getIndex(Nick n, List<Nick> l) {
        Nick nicks[] = l.toArray(new Nick[0]);

        int i = -1;
        for (int j = 0; j < nicks.length; j++) {
            if (nicks[j].getId().equals(n.getId())) {
                i = j;
            }
        }
        return i;
    }

    public String removeNick() {
        try {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbNicks.delete(nick);
            System.out.println("Handle removed");
            sessionBean.getEjbSession().getOwner().getNicks().remove(nick);
            return null;
        } catch (Exception ex) {
            Logger.getLogger(NicksBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
